package com.mydemo.controller;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
public class StreamingOutputController {

    /**
     * 通过 chatmodel 实现流式响应
     */
    @Resource(name="deepseek")
    private ChatModel deepSeekModel;

    @Resource(name="qwen")
    private ChatModel qwenModel;
    @GetMapping("/deepseek")
    public Flux<String> hello1(@RequestParam(name="msg",defaultValue = "你是谁" )String msg){
        return deepSeekModel.stream(msg);
    }

    @GetMapping("/qwen")
    public Flux<String> hello2(@RequestParam(name="msg",defaultValue = "你是谁" )String msg){
        return qwenModel.stream(msg);
    }

    /**
     * 通过 chatclient 实现流式响应
     */
    @Resource(name="deepSeekChatClient")
    private ChatClient deepSeekClient;
    @Resource(name="qwenChatClient")
    private ChatClient qwenClient;


    @GetMapping("/deepSeekChatClient")
    public Flux<String> hello3(@RequestParam(name="msg",defaultValue = "你是谁" )String msg){
        Flux<String> result = deepSeekClient.prompt(msg).stream().content();
        return result;
    }

    /**
     * produces = "text/event-stream;charset=UTF-8"
     * 添加 produces 属性，并指定返回的媒体类型为 text/event-stream 流式响应数据给前端
     * @param msg
     * @return
     */
    @GetMapping(value = "/qwenChatClient", produces = "text/event-stream;charset=UTF-8")
    public Flux<String> hello4(@RequestParam(name="msg",defaultValue = "你是谁" )String msg){
        Flux<String> result = qwenClient.prompt(msg).stream().content();
        return result;
    }

}
